<template>
  <div class="login-page">
    <el-card>
      <template #header>黑马面经运营后台</template>
      <el-form
        :model="user"
        :rules="rules"
        autocomplete="off"
        label-width="60px"
        ref="form"
      >
        <el-form-item prop="username" label="账号">
          <el-input v-model="user.username" placeholder="输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="username" label="密码">
          <el-input
            v-model="user.password"
            type="password"
            placeholder="输入用户密码"
          ></el-input>
        </el-form-item>
        <el-form class="tc">
          <el-button type="primary" @click="onSubmit">登录</el-button>
          <el-button>重置</el-button>
        </el-form>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { loginAPI } from '@/api/user'
export default {
  name: 'LoginIndex',
  data () {
    return {
      user: {
        username: 'admin',
        password: 'admin'
      },
      rules: {
        username: [
          {
            required: true,
            message: '请输入账号',
            trigger: ['blur', 'change']
          },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: ['blur', 'change']
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: ['blur', 'change']
          },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  methods: {
    onSubmit () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          // 登录成功
          // console.log(this.user)
          // 1 提示成功
          this.$message.success('恭喜登录成功')
          // 2 提交数据
          const res = await loginAPI(this.user)
          console.log(res)
          this.$store.commit('user/updateToken', res.data.token)
          // 3 跳转页面
          this.$router.push('/dashboard')
        } else {
          // console.log(this.user)
        }
      })
    }
  }
}
</script>

<style  scoped lang="scss">
.login-page {
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .el-card {
    width: 420px;
    ::v-deep .el-card__header {
      height: 80px;
      background: rgba(114, 124, 245, 1);
      text-align: center;
      line-height: 40px;
      color: #fff;
      font-size: 18px;
    }
  }
  .el-form {
    padding: 0 20px;
  }
  .tc {
    text-align: center;
  }
}
</style>
